#include <stdafx.h>

#include "T2Entity.h"
#include "Wall.h"
#include "TSquare.h"

namespace TSquare {
	using namespace System::Drawing;

	TSquare::TSquare(){
		walls = gcnew Generic::List<Wall^>();
		Cursor = Cursors::Cross;
		SetStyle(ControlStyles::DoubleBuffer, true);
	}

	void TSquare::OnPaint(PaintEventArgs ^e){
		Control::OnPaint(e);

		//e->Graphics->DrawEllipse(Pens::Black, Rectangle(10, 10, 200, 120));

		/*
		TODO:
		
		for each layer:
			if layer is active:
				retrieve the layer's objects from database and render
				render any temporary objects specific to this layer

		if selected object count > 0:
			render object handles

		if snap is enabled and cursor within range of a snap point:
			render snap glyph
		*/

		Graphics ^g = e->Graphics;

		g->SmoothingMode =
			System::Drawing::Drawing2D::SmoothingMode::AntiAlias;
		g->Clear(Color::White);

		for each(Wall ^wall in walls)
			wall->Draw(g);

		if(p1 != nullptr && p2 != nullptr){
			g->DrawLine(Pens::Black, *p1, *p2);
		}
	}

	void TSquare::OnMouseDown(MouseEventArgs ^e){
		if(p1 == nullptr){
			p1 = PointF((float)e->Location.X, (float)e->Location.Y);
			return;
		}
		else {
			p2 = PointF((float)e->Location.X, (float)e->Location.Y);
			Wall ^wall = gcnew Wall(*p1, *p2, 12);
			walls->Add(wall);

			p1 = nullptr;
			p2 = nullptr;
		}
	}

	void TSquare::OnMouseMove(MouseEventArgs ^e){
		p2 = PointF((float)e->Location.X, (float)e->Location.Y);
		Invalidate();
	}

}
